--region *.lua
--Date
--此文件由[BabeLua]插件自动生成
cc.exports.createMedal = function(score, best)
    if score < 5 then return nil end

    local level = 0
    if score <= best / 4 then
        level = 3
    elseif score <= best / 2 then level = 2
    elseif score <= best * 3 / 4 then level = 0
    else level = 1    
    end
    local medal =  cc.Sprite:create("medals_"..level..".png")
    local blink = nil
    
    if medal then
        blink = cc.Sprite:create("blink_00.png")
        local animation = cc.Animation:create()
        for i = 0, 2 do
            local star = "blink_0"..i..".png"
            animation:addSpriteFrameWithFile(star)
        end
        animation:setLoops(1000)
        animation:setDelayPerUnit(0.3)
        local blinking = cc.Animate:create(animation)
        blink:runAction(blinking)
    end
    
    local function getMedal()
        return medal
    end

    local function getBlink()
        return blink
    end
    
    local function blinking(delta)
        math.randomseed(os.time())
        local mX, mY = math.random() - 0.5, math.random() - 0.5
        local deltaX = medal:getContentSize().width * mX
        local deltaY = medal:getContentSize().height * mY
        blink:setPosition(cc.p(medal:getPositionX() + deltaX, medal:getPositionY() + deltaY))
        blink:setVisible(true)
    end

    return {getMedal = getMedal,
            getBlink = getBlink,
            blinking = blinking
        }
end

--endregion
